package com.basker.pisces.domain.element.meta;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.basker.pisces.core.meta.annotation.CollectionField;
import com.basker.pisces.core.meta.annotation.DataObjectMeta;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@DataObjectMeta
public class MetaType extends ElementType {

    @CollectionField
    private List<ElementType> properyElements = new ArrayList<>();

    @CollectionField
    private List<ElementType> commandElements = new ArrayList<>();

    @CollectionField
    private List<ElementType> validationElements = new ArrayList<>();

    private Map<String, ElementType> elementTypeMap = new HashMap<>();

    public void build() {
        elementTypeMap.clear();

        this.build(Arrays.asList(this));
        this.build(properyElements);
        this.build(commandElements);
        this.build(validationElements);
    }

    public ElementType getElementType(String name) {
        return this.elementTypeMap.get(name);
    }

    private void build(List<ElementType> elements) {
        for (ElementType elementType : elements) {
            elementTypeMap.put(elementType.getName(), elementType);
        }
    }

}
